<template>
  <div class="basic-info-container">
    <div class="content">
      <div style="marginTop:-18px;paddingTop:5px">
        <input
          type="text"
          v-model="basicInfo.name"
          class="input_dash mainname"
          @change="onNameChange"
        />
        <input
          type="text"
          v-model="basicInfo.jobIntension"
          class="input_dash jobIntension"
          @change="onJobIntensionChange"
        />
      </div>

      <TitleB
        :title="basicInfo.moduleTitle"
        iconName="el-icon-user"
        :backgroundColor="myThemeColor"
        @btnClick="onBtnClick"
        @titleBValUpdate="onModuleTitleUpdate"
      ></TitleB>
      <InfoDesc
        v-for="(info, index) in basicInfo.infoList"
        :key="index"
        :infos="info"
        @infoDescUpdate="onBasicInfoUpdate(index, $event)"
      ></InfoDesc>
    </div>
    <div class="avater-box">
      <el-upload
        class="avatar-uploader"
        action="#"
        :auto-upload="false"
        :on-change="onAvaterChange"
        :show-file-list="false"
      >
        <img v-if="basicInfo.imgUrl" :src="basicInfo.imgUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </div>
  </div>
</template>
<script>
import TitleB from '@/components/base/TitleB.vue'
import InfoDesc from '@/components/base/InfoDesc.vue'
import { nanoid } from 'nanoid'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      basicInfo: {
        moduleTitle: '',
        name: '',
        jobIntension: '',
        infoList: [],
        imgUrl: '',
      },
      id: '',
    }
  },
  props: {
    basicInfoProps: {
      type: Object,
      default: function () {
        return {
          moduleTitle: '个人信息',
          infoList: [
            [
              {
                title: '姓名',
                value: '庞巧玲',
              },
              {
                title: '性别',
                value: '女',
              },
            ],
            [
              {
                title: '年龄',
                value: '23',
              },
              {
                title: '经验',
                value: '应届生',
              },
            ],
            [
              {
                title: '手机',
                value: '15868878155',
              },
              {
                title: '电子游戏',
                value: 'lemon_pql@163.com',
              },
            ],
          ],
        }
      },
    },
    moduleId: {
      type: String,
      default: nanoid(),
    },
  },
  computed: {
    ...mapGetters(['myThemeColor']),
  },
  created() {
    this.basicInfo = { ...this.basicInfoProps }
    this.id = this.moduleId
  },
  methods: {
    // 修改模块标题
    onModuleTitleUpdate(newTitle) {
      this.basicInfo.moduleTitle = newTitle
      this.updateStore()
    },
    // 修改姓名
    onNameChange() {
      this.updateStore()
    },
    // 修改职位
    onJobIntensionChange() {
      this.updateStore()
    },
    // 修改个人信息
    onBasicInfoUpdate(index, newInfos) {
      this.basicInfo.infoList[index] = newInfos
      this.updateStore()
    },
    onBtnClick(tag) {
      switch (tag) {
        case 'add':
          let info = [
            {
              title: '新建：',
              value: '自定义',
            },
            {
              title: '新建：',
              value: '自定义',
            },
          ]
          this.basicInfo.infoList.push(info)
          console.log(this.basicInfo.infoList);
          this.updateStore()
          break
        case 'delete':
          if (this.basicInfo.infoList.length > 1) {
            this.basicInfo.infoList.pop()
            this.updateStore()
          } else this.$message.warning('至少需要一条数据')
          break
        case 'remove':
          this.$store.commit('removeModule', this.id)
          break
      }
    },
    // 修改照片信息
    onAvaterChange(file, fileList) {
      var _this = this
      var event = event || window.event
      var file = event.target.files[0]
      var reader = new FileReader()
      //转base64
      reader.onload = function (e) {
        _this.basicInfo.imgUrl = e.target.result //将图片路径赋值给src
        _this.updateStore()
      }
      reader.readAsDataURL(file)
    },
    // 更新仓库数据
    updateStore() {
      const newData = { props: this.basicInfo, id: this.id }
      this.$store.commit('setModule', newData)
    },
  },
  components: {
    TitleB,
    InfoDesc,
  },
}
</script>

<style scoped lang="less">
.basic-info-container {
  width: 100%;
  padding: 0 15px;
  margin-bottom: 5px;
  display: flex;
  justify-content: space-between;
}
.content {
  flex: 8;
}
.item {
  padding: 10px 5px;
  display: flex;
  justify-content: space-around;
  padding: 0;
  font-size: 14px;
}

.avater-box {
  flex: 2;
  width: 150px;
  height: 150px;
}
.avatar-uploader,
.el-upload {
  cursor: pointer;
  position: relative;
  overflow: hidden;
  text-align: center;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  // margin-top: 3px;
  font-size: 28px;
  color: #8c939d;
  width: 2.5cm;
  height: 3.5cm;
  line-height: 3.5cm;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar {
  width: 2.5cm;
  height: 3.5cm;
  display: block;
}
.input_dash {
  border-width: 0ch;
  border-color: transparent;
  border-width: 1px;
  border-style: dotted;
  font-size: 23px;
  height: 45px;
  width: 250px;
  background-color: transparent;
  color: black;
  line-height: 45px;
}
.input_dash:focus {
  background: none;
  outline: none;
  border: 0px;
  border-color: tomato;
  border-width: 1px;
  border-style: dotted;
}
.mainname {
  margin-left:10px;
}
.jobIntension {
  font-size: 18px;
}
</style>
